﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DBHelper.Model;
using DBHelper.DAL;
using DBHelper.BLL;
using System.Text.RegularExpressions;
using System.Web.UI.MobileControls;
using System.Collections.Generic;
using System.IO;
using Maticsoft.Common;

namespace LongQuanWeb.Sheet
{
    public partial class UserRegister : System.Web.UI.Page
    {
        private DBHelper.BLL.Users m_BllUser = null;
        private DBHelper.Model.Users m_User = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            m_BllUser = new DBHelper.BLL.Users();
            DDL_Sex.SelectedIndex = 0;
            DDL_Education.SelectedIndex = 0;
            m_User = new DBHelper.Model.Users();
        }

        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            List<DBHelper.Model.Users> judgeuser = m_BllUser.GetModelList(" UserID = '" + Tbx_UserID.Text.Trim() + "'");
            if (judgeuser.Count > 0)
            {
                Response.Write("<script>alert('对不起，该用户名已经被注册！');</script>");
                return;
            }
            Regex regex = null;
            m_User.UserID = Tbx_UserID.Text.Trim();
            m_User.UserName = Tbx_UserName.Text;
            m_User.UserPassword = Tbx_Password.Text;
            regex= new Regex(@"^(^\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$");
            m_User.Sex = DDL_Sex.Text;
            string IDCard = Tbx_IDCode.Text.Trim();
            if(!regex.IsMatch(IDCard))
            {
                Response.Write("<script>alert('身份证号码不正确！');</script>");
                return;
            }
            int birthday=0;
             if (IDCard.Length == 15)       
             {
                 birthday = int.Parse(IDCard.Substring(6, 2));
                 
             }
             else if (IDCard.Length == 18)
             {
                 birthday = int.Parse(IDCard.Substring(8, 2));
             }
            int age = DateTime.Now.Year - 1900 - birthday;
            if (age < 18 || age > 35)
            {
                Response.Write("<script>alert('身份证验证失败：注册者年龄必须在18~35周岁之间！');</script>");
                return;
            }
            int year;
            bool rc = int.TryParse(Tbx_GraduateYear.Text.Trim(), out year);
            if (!rc || ((year > DateTime.Now.Year) || (year < 2000)))
            {
                Response.Write("<script>alert('年份必须为数字并且合法！');</script>");
                return;
            }
            m_User.GraduateYear = year;
            m_User.GraduateCollege = Tbx_College.Text.Trim();
            m_User.Major = Tbx_Major.Text.Trim();
            m_User.Education = int.Parse(DDL_Education.SelectedValue);
            m_User.RegAddress = Tbx_RegAddress.Text.Trim();
            m_User.CurAddress = Tbx_CurAddress.Text.Trim();
            long mobile;
            rc = long.TryParse(Tbx_MobileNum.Text.Trim(), out mobile);
            regex = new Regex(@"^13\d{9}|159\d{8}|153\d{8}");
            if (!rc || (!(Tbx_MobileNum.Text.Length == 11 && regex.IsMatch(Tbx_MobileNum.Text.Trim()))))
            {
                Response.Write("<script>alert('手机号必须为数字，长度为11位并且有效！');</script>");
                return;
            }
            m_User.Mobile = mobile;
            long tel;
            rc = long.TryParse(Tbx_TelNum.Text.Trim(), out tel);
            if (!rc || (Tbx_TelNum.Text.Length > 12 || Tbx_TelNum.Text.Length < 6))
            {
                Response.Write("<script>alert('电话号码长度在6~12位之间，并且应该全部为数字');</script>");
                return;
            }
            m_User.Tel = tel;
            string email = Tbox_Email.Text.Trim();
            regex = new Regex(@"^[a-zA-Z]\w+@\w+\.\w+$");
            if (!regex.IsMatch(email))
            {
                Response.Write("<script>alert('email格式不正确，请重新输入');</script>");
                return;
            }
            m_User.Email = email;
            m_User.QQ = Tbx_QQ.Text.Trim();
            m_User.Isemployed = int.Parse(RBL_IsEmployed.SelectedValue);
            m_User.InCity = int.Parse(RBL_IsInCity.SelectedValue);
            m_User.CompanyType = RBL_CompanyType.SelectedValue;
            m_User.CurCompanyName = Tbx_CurCompanyName.Text.Trim();
            if (RBL_UnemployedReason.SelectedItem.Text.Trim() == "其他")
            {
                m_User.unEmployedReason = Tbx_OhterReason.Text.Trim();
            }
            else
                m_User.unEmployedReason = RBL_UnemployedReason.SelectedItem.Text.Trim();
            m_User.Status = "已提交";
            m_User.Mark = Tbx_Mark.Text.Trim(); 
            string userpath = Request.PhysicalApplicationPath;
            if (!System.IO.Directory.Exists(userpath + @"document\" + Tbx_UserID.Text.Trim()))
            {
                Directory.CreateDirectory(userpath + Tbx_UserID.Text.Trim());
            }
            m_User.DocPath = userpath + Tbx_UserID.Text.Trim();
            int result = m_BllUser.Add(m_User);
            if (result > 0)
            {
                Maticsoft.Common.MessageBox.Show(this, "提交成功");
            }
            else
                MessageBox.Show(this, "提交失败！网络存在异常！");
            Session["UserID"] = m_User.UserID;
            Session["UserStatus"] = m_User.Status;
            Session["UserPassword"] = m_User.UserPassword;
            Response.Redirect("RegSuccess.aspx");
        }

        protected void TextBox14_TextChanged(object sender, EventArgs e)
        {

        }

        protected void Tbx_IDCode_TextChanged(object sender, EventArgs e)
        {
        
        }

        protected void Tbx_UserName_TextChanged(object sender, EventArgs e)
        {

        }

        protected void DDL_Education_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void Tbx_CurCompanyName_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
